package lab_08.com.acme.task6;

public class CommentsRemover {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Wrong number of arguments. One argument is expected.");
			return;
		}
		String str = args[0];
		process(str, 0, false);
	}
		
	/**
	 * @param str string to process
	 * @param starting_pos position to begin processing with
	 * @param ignore_multiline_comment ignore /*... comments
	 */
	private static void process(String str, int starting_pos, boolean ignore_multiline_comment) {
		boolean single_line_comment_mode = false;
		boolean multi_line_comment_mode = false;
		int comment_start_pos = 0;
		char current_char;
		int length = str.length();
		
		for (int i = starting_pos; i < length; ++i) {
			current_char = str.charAt(i);
			
			// Multiline comment is started but not closed with */
			if (i+1 >= length && multi_line_comment_mode) { //is current_char the last symbol of the string
				process(str, comment_start_pos, true);
				return;
			}
			//switch off one-line comment mode
			if (single_line_comment_mode) {
				if (current_char == '\n') {
						single_line_comment_mode = false;
				}
			}
			// switch off multiline comment mode
			else if (multi_line_comment_mode) {
				if (current_char == '*' && str.charAt(i + 1) == '/') { //no check for out-of-bounds as we worked this case above
					multi_line_comment_mode = false;
					i++; // skip the next symbol /
				}
			// start one line comment
			}
			else if (current_char == '/' && i+1 < length && str.charAt(i+1) == '/') {
				single_line_comment_mode = true;
				i++; //skip the next symbol as it's /
			}
			else if (!ignore_multiline_comment && current_char == '/' && i+1 < length && str.charAt(i+1) == '*') {
				multi_line_comment_mode = true;
				comment_start_pos = i;
				i++; //skip the next symbol as it's * denoting the beginning of the comment
			}
			else {
				System.out.print(current_char);
			}
				
		}
	}
}
